/**
 * Created by chenzheyu on 2017/4/7.
 */

$(function () {

    var limit = 20;
    var SEOval = '';
    var isGroundings = '';
    var shopId = '';
    var DownPullSelectOption = {
        data: [{
            name: '全部门店',
            Val: ''
        }],
        onChange: function (e) {
            shopId = e.val;
        }
    };

    $.ajax({
        url: ctx + '/rentshop/selectRentshopByShopName',
        data: {},
        async: false,
        success: function (data) {
            for (var i = 0; i < data.content.length; i++) {
                (function () {
                    var index = i;
                    DownPullSelectOption.data.push({
                        name: data.content[index].shopName,
                        Val: data.content[index].id
                    })
                })()
            }
        }
    });

    $('.isGroundingsSelects').on('change', function () {
        isGroundings = $(this).val()
    });

    $('.shopSelect').DownPullSelect(DownPullSelectOption);

    $('.SEOButton').on('click', function () {
        SEOval = $('.SEO input').val();
        CarOrder(1, SEOval, isGroundings, shopId)
    });

    $('.AddCar').ActionButtons({
        data: '+ 添加车辆',
        onChange: function () {
            window.location.href = ctx + '/rentcar/add'
        }
    });

    function CarOrder(page, SEOval, isGroundings, shopId) {
        $
            .ajax({
                url: ctx + '/rentcar/findCar',
                data: {
                    shopId: shopId,
                    isGrounding: isGroundings,
                    param: SEOval,
                    'limit': limit,
                    "pager.offset": (page - 1) * limit
                },
                type: 'GET',
                success: function (data) {

                    var OrderOptions = {
                        data: {
                            Thead: {
                                Action: '操作',
                                CarName: '车辆名称',
                                State: '状态',
                                Rent: '租金',
                                CashPledge: '违章押金',
                                pregrant: '预授权额度',
                                Brand: '品牌',
                                PlateNumber: '车牌号',
                                BelongStore: '所属门店',
                                gearbox: '变速箱',
                                CarModel: '车型',
                                CarEmissions: '排量',
                                SeatingNumber: '座位数',
                                SteerMileage: '行驶里程',
                                PurchaseTime: '购置时间',
                                Location: '所在地',
                                Restrict: '限行',

                            },
                            Tbody: []
                        },
                    };

                    for (var i = 0; i < data.content.length; i++) {
                        (function () {
                            var index = i;
                            var val = {
                                Action: '',
                                CarName: data.content[index].rentCar.carName,
                                State: '',
                                Rent: data.content[index].vehicle.rent,
                                CashPledge: data.content[index].vehicle.illegalDeposit,
                                pregrant: data.content[index].vehicle.pregrant,
                                Brand: data.content[index].vehicle.brandName,
                                PlateNumber: data.content[index].car.plateNo,
                                BelongStore: data.content[index].rentCar.shopName,
                                gearbox: data.content[index].vehicle.transmission,
                                CarModel: data.content[index].vehicle.type,
                                CarEmissions: data.content[index].vehicle.displacement,
                                SeatingNumber: data.content[index].vehicle.seat,
                                SteerMileage: data.content[index].car.mileage,
                                PurchaseTime: $().Timestamp.TimestampDate(data.content[index].rentCar.purchase),
                                Location: '',
                                Restrict: ''
                            };


                            var atcion = $('<span class="action CustomClear"></span>');
                            var redact = $('<span class="AcRedact"></span>');
                            redact
                                .ActionButtons({
                                    data: '编辑',
                                    onChange: function () {
                                        console
                                            .log(data.content[index].rentCar.id)
                                        window.location.href = ctx
                                            + '/rentcar/update?id='
                                            + data.content[index].rentCar.id;
                                    }
                                });
                            atcion.append(redact);
                            var isGrounding = $('<span class="isGrounding"></span>');

                            switch (data.content[index].rentCar.state) {
                                case 0:
                                    val.State = $().Badge({
                                        data: {
                                            text: '空闲',
                                            state: "Ok"
                                        }
                                    });
                                    switch (data.content[index].car.isGrounding) {
                                        case 0:
                                            isGrounding
                                                .ActionButtons({
                                                    data: '上架',
                                                    onChange: function () {
                                                        $
                                                            .ajax({
                                                                url: ctx
                                                                + '/rentcar/updateGrounding',
                                                                data: {
                                                                    id: data.content[index].rentCar.id,
                                                                    isGrounding: 1
                                                                },
                                                                type: 'POST',
                                                                success: function (data) {
                                                                    if (data.message == '车辆上架成功') {
                                                                        $(
                                                                            'body')
                                                                            .Overlays(
                                                                                {
                                                                                    data: {
                                                                                        text: "车辆上架成功！",
                                                                                        state: 'Ok'
                                                                                    }
                                                                                });
                                                                        CarOrder(
                                                                            1,
                                                                            SEOval,
                                                                            isGroundings,
                                                                            shopId)
                                                                    } else {
                                                                        $(
                                                                            'body')
                                                                            .Overlays(
                                                                                {
                                                                                    data: {
                                                                                        text: "车辆上架失败,请稍后重试！",
                                                                                        state: 'No'
                                                                                    }
                                                                                });
                                                                    }
                                                                }
                                                            })
                                                    }
                                                });
                                            atcion.append(isGrounding);
                                            break;
                                        case 1:

                                            isGrounding
                                                .ActionButtons({
                                                    data: '下架',
                                                    onChange: function () {
                                                        $
                                                            .ajax({
                                                                url: ctx
                                                                + '/rentcar/updateGrounding',
                                                                data: {
                                                                    id: data.content[index].rentCar.id,
                                                                    isGrounding: 0
                                                                },
                                                                type: 'POST',
                                                                success: function (data) {
                                                                    if (data.message == '车辆下架成功') {
                                                                        $(
                                                                            'body')
                                                                            .Overlays(
                                                                                {
                                                                                    data: {
                                                                                        text: "车辆下架成功！",
                                                                                        state: 'Ok'
                                                                                    }
                                                                                });
                                                                        CarOrder(
                                                                            1,
                                                                            SEOval,
                                                                            isGroundings,
                                                                            shopId)
                                                                    } else {
                                                                        $(
                                                                            'body')
                                                                            .Overlays(
                                                                                {
                                                                                    data: {
                                                                                        text: "车辆下架失败,请稍后重试！",
                                                                                        state: 'No'
                                                                                    }
                                                                                });
                                                                    }
                                                                }
                                                            })
                                                    }
                                                });
                                            atcion.append(isGrounding);
                                            break;
                                    }
                                    break;
                                case 1:
                                    val.State = $().Badge({
                                        data: {
                                            text: '忙碌',
                                            state: "Warn"
                                        }
                                    });
                                    switch (data.content[index].car.isGrounding) {
                                        case 0:
                                            isGrounding.ActionButtons({
                                                data: '上架',
                                                onChange: function () {
                                                    $('body').Overlays(
                                                        {
                                                            data: {
                                                                text: "忙碌状态的车辆禁止下架！",
                                                                state: 'No'
                                                            }
                                                        });
                                                }
                                            });
                                            atcion.append(isGrounding);
                                            break;
                                        case 1:
                                            isGrounding.ActionButtons({
                                                data: '下架',
                                                onChange: function () {
                                                    $('body').Overlays(
                                                        {
                                                            data: {
                                                                text: "忙碌状态的车辆禁止下架！",
                                                                state: 'No'
                                                            }
                                                        });
                                                }
                                            });
                                            atcion.append(isGrounding);
                                            break;
                                    }
                                    break;
                                default:
                                    val.State = $().Badge({
                                        data: {
                                            text: '异常',
                                            state: "No"
                                        }
                                    });
                                    switch (data.content[index].car.isGrounding) {
                                        case 0:
                                            isGrounding.ActionButtons({
                                                data: '上架',
                                                onChange: function () {
                                                    $('body').Overlays(
                                                        {
                                                            data: {
                                                                text: "异常状态的车辆禁止下架！",
                                                                state: 'No'
                                                            }
                                                        });
                                                }
                                            });
                                            atcion.append(isGrounding);
                                            break;
                                        case 1:
                                            isGrounding.ActionButtons({
                                                data: '下架',
                                                onChange: function () {
                                                    $('body').Overlays(
                                                        {
                                                            data: {
                                                                text: "异常状态的车辆禁止下架！",
                                                                state: 'No'
                                                            }
                                                        });
                                                }
                                            });
                                            atcion.append(isGrounding);
                                            break;
                                    }
                            }

                            switch (data.content[index].rentCar.limit) {
                                case 0:
                                    val.Restrict = '不限行';
                                    break;
                                case 1:
                                    val.Restrict = '星期一';
                                    break;
                                case 2:
                                    val.Restrict = '星期二';
                                    break;
                                case 3:
                                    val.Restrict = '星期三';
                                    break;
                                case 4:
                                    val.Restrict = '星期四';
                                    break;
                                case 5:
                                    val.Restrict = '星期五';
                                    break;
                                case 6:
                                    val.Restrict = '星期六';
                                    break;
                                case 7:
                                    val.Restrict = '星期日';
                                    break;
                            }


                            val.Action = atcion;
                            $.ajax({
                                url: ctx + '/rentshop/selectRentshop',
                                data: {
                                    id: data.content[index].rentCar.shopId
                                },
                                async: false,
                                type: 'GET',
                                success: function (data) {
                                    val.Location = data.city + data.address
                                }
                            });

                            OrderOptions.data.Tbody.push(val)

                        })()
                    }

                    $(".TableVal").Order(OrderOptions);

                    var PagingMdOption = {
                        data: {
                            rows: limit,
                            nowrows: page,
                            Pages: '',
                            total: data.totalElements,
                            change: function (index) {
                                CarOrder(index, SEOval, isGroundings,
                                    shopId)
                            }
                        }
                    };

                    $('.TableVal').PagingMd(PagingMdOption);

                }
            })
    }

    CarOrder(1, SEOval);

    $(document).keyup(function (event) {
        switch (event.keyCode) {
            case 13:
                $(".SEOButton").click();
                break;
        }
    });

});
